import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';

import 'model/chart_dot_data.dart';

/// @Title   只有单颜色折线的折线图组件
/// @Author: bryce
/// @CreateDate:  2023/8/16 15:18
class ChartSingleColorLineWidget extends StatefulWidget {
  final List<ChartDotData> dots;
  final double height;
  final double? width;

  // 线颜色和lineGradient二选一不能同时传
  final Color? lineColor;
  final Gradient? lineGradient;
  final double barWidth;

  /// 防止弯曲的时候越界(默认为true,不建议修改，除非有特殊需求需要展示大平滑度)
  final bool preventCurveOverShooting;

  /// 曲线平滑度值，默认0.35，当isCurve为true时才有效
  final double curveSmoothness;

  /// 折线是否弯曲
  final bool isCurved;

  const ChartSingleColorLineWidget(
      {super.key,
      required this.dots,
      this.lineColor,
      this.lineGradient,
      this.barWidth = 2,
      this.width,
      this.height = 300,
      this.preventCurveOverShooting = true,
      this.curveSmoothness = 0.35,
      this.isCurved = true});

  @override
  State<StatefulWidget> createState() => _ChartSingleColorLineWidgetState();
}

class _ChartSingleColorLineWidgetState extends State<ChartSingleColorLineWidget> {
  // 用于分隔上下不同颜色的基准数值，例如（以0分隔，0以上的线是收入，为红色，以下为支出，为蓝色）
  double cuteY = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(height: widget.height, width: widget.width, child: LineChart(lineChatData()));
  }

  LineChartData lineChatData() {
    return LineChartData(
      // 触摸时的气泡和指示器
      lineTouchData: LineTouchData(enabled: false),
      // 中间的网格
      gridData: FlGridData(show: false),
      titlesData: FlTitlesData(show: false),
      // 边框
      borderData: FlBorderData(show: false),
      // 折线数据，包括数据、颜色、填充、样式
      lineBarsData: [
        LineChartBarData(
            spots: widget.dots.asMap().entries.map((e) => FlSpot(e.key.toDouble(), e.value.value ?? 0)).toList(),
            // 是否弯曲
            isCurved: widget.isCurved,
            curveSmoothness: widget.curveSmoothness,
            // 折线颜色
            color: widget.lineColor,
            gradient: widget.lineGradient,
            barWidth: widget.barWidth,
            isStrokeCapRound: true,
            dotData: FlDotData(show: false)),
      ],
    );
  }
}
